﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author Oplink OMS
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webems.modules.log.service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.common.utils.DateUtils;
import com.oplink.webems.modules.log.aop.AopModulesConstant;
import com.oplink.webems.modules.log.dao.UserLogDao;
import com.oplink.webems.modules.log.entity.UserLog;

/**
 * 告警Service
 * 
 * @author Oplink OMS
 * @version 2014-09-09
 */
@Component
@Transactional
public class UserLogService {
	
	/*
	delCurRow1       |      3
	find             |      3
	find             |    301
	find             |    302
	findByTopoId     |      2
	getAlarmprop     |    302
	getAlarmPropJson |    302
	getAllAlarmType  |    302
	getCurrentAlarm  |      3
	getStatMap       |    301
	save             |      2
	statAlarm        |    301
	synAlarmSave     |      3
	updateTopo       |      2*/
	
	public static final String ALARM_DELCURROW = AopModulesConstant.ALARM+"_delCurRow1";
	public static final String ALARM_FIND = AopModulesConstant.ALARM+"_find";
	public static final String ALARM_GET = AopModulesConstant.ALARM+"_getCurrentAlarm";
	public static final String ALARM_SYN = AopModulesConstant.ALARM+"_synAlarmSave";

	public static final String ALARMHIS_FIND = AopModulesConstant.ALARM_HIS+"_find";
	public static final String ALARMHIS_GET = AopModulesConstant.ALARM_HIS+"_getStatMap";
	public static final String ALARMHIS_STAT = AopModulesConstant.ALARM_HIS+"_statAlarm";
	
	public static final String ALARMPROP_FIND = AopModulesConstant.ALARM_PROP+"_find";
	public static final String ALARMPROP_GET = AopModulesConstant.ALARM_PROP+"_getAlarmprop";
	public static final String ALARMPROP_QUERY = AopModulesConstant.ALARM_PROP+"_getAlarmPropJson";
	public static final String ALARMPROP_GETTYPE = AopModulesConstant.ALARM_PROP+"_getAllAlarmType";
	
	public static final String TOPO_GET = AopModulesConstant.TOPO+"_findByTopoId";
	public static final String TOPO_SAVE = AopModulesConstant.TOPO+"_save";
	public static final String TOPO_UPDATE = AopModulesConstant.TOPO+"_updateTopo";
	
	public static final Map<String, String> userLogDesc = new HashMap<String, String>(){
		private static final long serialVersionUID = 199674582702825401L;
		{
			put(ALARM_DELCURROW, "删除告警");
			put(ALARM_FIND, "查询告警");
			put(ALARM_GET, "获得单个告警");
			put(ALARM_SYN, "同步告警");
			put(ALARMHIS_FIND, "查找历史告警");
			put(ALARMHIS_GET, "获取历史告警");
			put(ALARMPROP_FIND, "获取告警属性");
			put(ALARMPROP_GET, "获取告警属性");
			put(ALARMPROP_QUERY, "查询所有告警属性");
			put(ALARMPROP_QUERY, "获取所有告警类型");
			put(TOPO_GET, "获取拓扑信息");
			put(TOPO_SAVE, "保存拓扑信息");
			put(TOPO_UPDATE, "更新拓扑信息");
		}
	};

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(UserLogService.class);

	@Autowired
	private UserLogDao userLogDao;

	public UserLog get(Long id) {
		return userLogDao.findOne(id);
	}

	public Page<UserLog> find(Page<UserLog> page, UserLog userLog, String beginTime, String endTime) {
		DetachedCriteria dc = userLogDao.createDetachedCriteria();
		// 添加查询条件
		if ((null != userLog.getModule()) && StringUtils.isNotEmpty(userLog.getModule().toString())) {
			dc.add(Restrictions.eq("module", userLog.getModule()));
		}
		if (StringUtils.isNotEmpty(userLog.getDesci())) {
			dc.add(Restrictions.like("desci", "%" + userLog.getDesci() + "%"));
		}
		if (StringUtils.isNotEmpty(userLog.getParams())) {
			dc.add(Restrictions.like("params", "%" + userLog.getParams() + "%"));
		}
		if (StringUtils.isNotEmpty(beginTime)){
        	Calendar bg = DateUtils.getCalendar(beginTime);
        	if(bg!=null){
        		dc.add(Restrictions.ge("timei",bg));
        	}
			//dc.add(Restrictions.like("timei", "%"+commLog.getTimei()+"%"));
		}
        if (StringUtils.isNotEmpty(endTime)){
        	Calendar end = DateUtils.getCalendar(endTime);
        	if(end!=null){
        		dc.add(Restrictions.le("timei",end));
        	}
		}
		if (userLog.getOwnerId()!=null) {
			dc.add(Restrictions.eq("ownerId", userLog.getOwnerId()));
		}
		dc.add(Restrictions.eq(UserLog.DEL_FLAG, UserLog.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return userLogDao.find(page, dc);
	}

	/**
	 * @function 创建查询
	 **/
	public Page<UserLog> getUserUserLog(Page<UserLog> page, UserLog userLog,String beginTime, String endTime) {
		DetachedCriteria dc = userLogDao.createDetachedCriteria();
		// 添加查询条件
		if ((null != userLog.getModule()) && StringUtils.isNotEmpty(userLog.getModule().toString())) {
			dc.add(Restrictions.like("module", "%" + userLog.getModule() + "%"));
		}
		if (StringUtils.isNotEmpty(userLog.getDesci())) {
			dc.add(Restrictions.like("desc", "%" + userLog.getDesci() + "%"));
		}
		if (StringUtils.isNotEmpty(userLog.getParams())) {
			dc.add(Restrictions.like("params", "%" + userLog.getParams() + "%"));
		}
		if (StringUtils.isNotEmpty(beginTime)){
        	Calendar bg = DateUtils.getCalendar(beginTime);
        	if(bg!=null){
        		dc.add(Restrictions.ge("timei",bg));
        	}
			//dc.add(Restrictions.like("timei", "%"+commLog.getTimei()+"%"));
		}
        if (StringUtils.isNotEmpty(endTime)){
        	Calendar end = DateUtils.getCalendar(endTime);
        	if(end!=null){
        		dc.add(Restrictions.le("timei",end));
        	}
		}
		dc.add(Restrictions.eq(UserLog.DEL_FLAG, UserLog.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return userLogDao.find(page, dc);
	}

	public UserLog getUserLog(Long id) {
		return userLogDao.findOne(id);
	}

	public void saveUserLog(UserLog entity) {
		userLogDao.save(entity);
	}

	public void deleteUserLog(Long id) {
		userLogDao.delete(id);
	}

	@Autowired
	public void setUserLogDao(UserLogDao userLogDao) {
		this.userLogDao = userLogDao;
	}
	
	//-------------------------------------------------------//
	
	public int save(UserLog entity){
		return userLogDao.saveLog(entity);
	}
}
